<?php
class IndexAction extends Action
{
	//初始化载入函数
	protected function _initialize()
	{
		//
		$tablename =C('TABLENAME');
		$db = M($tablename);//这里设置数据库中的表名
		$dbtable = $db->query('SHOW TABLES');
		$this->assign('dbtable',$dbtable);

		$mn = M('pageinfo');
		$module = $mn->group('module')->select();
		$this->assign('module',$module);
	}


	/**
	 * function return action
	 * 
	 * @package index
	 * @access private
	 */
	function returna()
	{
		$code ='';
		//输出文件头部

		$modulena = trim($_REQUEST['pmodule']);
		//输出类头
		$code .='<?php
		class '.ucwords($modulena).'Action extends AppBaseAction
		{
		';

		$pn = M('pageinfo');
		$funclist = $pn->where("module='".$modulena."'")->field('func,funcname')->select();

		foreach ($funclist as $value)
		{
			$code .='
			';
			//输出方法体内容
			$code .= "	".'	function '.$value['func'].'()
			{
				//'.$value['funcname'].'
				$this->display();
			}';
			$code .='
			';
		}
		//输出类底
		$code .='
		} 
		?>';
		echo nl2br(htmlentities($code,ENT_NOQUOTES,'utf-8'));
	}

	//返回模型信息 
	//TODO:解决管理查询问题
	function returnm()
	{
		$model = trim($_REQUEST["dbtable"]);
		//dump($model);
		//exit;
		$tname = explode("_",$model);
		if(!empty($model))
		{
			$m = M($tname[1]);
			$field = $m->query("SHOW FULL FIELDS FROM ".$model);
		}
		$code ='<?php
		class '.$tname[1].'Model extends Model
		{
		';

		$code .='
		//验证规则array(b验证字段，b验证规则,b错误提示,验证条件 0存 1必 2不空,
		//附加规则regex function callback(当前类方法) confirm equal in unique,内置了require url currency number
		//验证时间 1新  2编  3全部M）
		
		//输出自动验证模型
		
		protected $_validate  = array(
		';
		foreach ($field as $value)
		{
			$code .="array ('".$value['Field']."','','',2),".'
			';
		}

		$code .='
		);
		
		//array(填充字段，填充内容，填充条件1新 M 2编 3全，附加规则function callback field stringM)
		// 自动填充设置 
		protected $_auto = array(
		';
		foreach ($field as $value)
		{
			$code .="array ('".$value['Field']."','',2,''),".'
			';
		}

		$code .='
		
		);';

		//输出VIEW
		$code .='
		//dump view model
		public $viewFields = array(
		
		'."'".$tname[1]."'".'=>array(
		';
		foreach ($field as $value)
		{
			$code .="'".$value['Field']."',";
		}
		$code .="'_on'=>'  '".'),
		);
		}
		?>';
		echo nl2br(htmlentities($code,ENT_NOQUOTES,'utf-8'));
	}

	//返回表单信息
	function returnr() {
		$model = trim($_REQUEST["dbtable"]);
		//dump($model);
		//exit;
		$tname = explode("_",$model);
		if(!empty($model))
		{
			$m = M($tname[1]);
			$field = $m->query("SHOW FULL FIELDS FROM ".$model);
		}
		//初始化
		$code ='';

		//表单生成，获取表单类型
		$tt = $_POST['tt'];
		if(!empty($tt))
		{
			switch ($tt)
			{
				case 'list':
					//需要配置表头和表尾信息
					if($_POST['adminc'] == 'on')
					    {
							$code .= '<div class="leftspace">
					<form id="form1" action="__URL__/" method="post">
					';
					    } else {
					    	$code.='front list';
					    }
					//输出列表头部信息
					foreach($field as $key=>$value)
					{
						$f =$value['Field'];
						if($_POST["c$f"] =='show')
						{
							if($_POST['adminc'])
							{
								$code .= '<th>';
								$code .=$value['Comment'];
								$code .= '</th>';
							}else {
								$code.='fronts';
							}
						}
					}
					//输出列表行
					$code .='列表行头部';
					foreach($field as $key=>$value)
					{
						$f =$value['Field'];
						if($_POST["c$f"] =='show')
						{
							if($_POST['adminc'])
							{
								//显示后台
								$code .= '<td>';
								$code .='{$vo.'.$value['Field'].'}';
								$code .= '</td>';
							} else {
								//TODO:显示前台
								$code .= 'front';
								
							}
						}
					}
					if($_POST['adminc'] == 'on')
					{
						$code .= '<td><a href="__URL__/edi_'.$tname[1].'/id/{$vo.id}">编辑</a></td></tr></iterate></table>';
					}
					$code .='列表行底部';
					$code .='
					</form>
					</div>';
					break;

				case 'add':
					if($_POST['adminc'] == 'on')
					{
					$code .= '<tagLib name="html,cx" />
					<include file="Public:header" />
					<div class="bodyTitle">
					<div class="bodyTitleLeft"></div>
					 <div class="bodyTitleText">添加</div>
					</div>
					<br><h3 class="marginbot"><a href="__URL__/'.$tname[1].'index" class="sgbtn">＋返回主类</a></h3>
					<table width="100%">
					<form id="form1" action="__URL__/ins_'.$tname[1].'" method="post">
					
					';
					}else {
						$code .='front table info';
					}
							
					foreach($field as $key=>$value)
					{
						
						//$code .= '<li>';
						$f = $value['Field'];
						//输出控件类型
						if($_POST['adminc'] == 'on')
					    {
							if($_POST["c$f"] =='input')
							{
								 $code .='<tr><td>'.$value['Comment'].'</td><td>    <input name="'.$value['Field'].'" value="" type="text"  /> </td></tr>';
							} else if($_POST["c$f"] =='select') {
								 $code .='<tr><td>'.$value['Comment'].'</td><td>    <select name="'.$value['Field'].'"'.' class="w-100"><option value="0">系统根ID</option><volist name="list" id="svo"><option value="{$svo.id}" >{$svo.fulltitle}</option></volist></select>';
							} else if($_POST["c$f"] =='radio'){
								 $code .='<tr><td>'.$value['Comment'].'</td><td>    <input type="radio" name="'.$value['Field'].'" value="1" />有 <input type="radio" name="'.$value['Field'].'" value="2" />无</td></tr>';
							} else if($_POST["c$f"] == 'textarea') {
								$code .='<tr><td>'.$value['Comment'].'</td><td>    <textarea id="'.$value['Field'].'" rows="10" cols="40" name="'.$value['Field'].'"></textarea></td></tr>';
				
							}
					    }else {
					    	//TODO: show front
						    if($_POST["c$f"] =='input')
								{
									 $code .='<span>'.$value['Comment'].'</span>    <input name="'.$value['Field'].'" value="" type="text"  /> <br />';
								} else if($_POST["c$f"] =='select') {
									 $code .='<span>'.$value['Comment'].'</span>    <select name="'.$value['Field'].'"'.' class="w-100"><option value="test">test</option>'.'</select>';
								} else if($_POST["c$f"] =='radio'){
									 $code .='<span>'.$value['Comment'].'</span>    <input type="radio" name="'.$value['Field'].'" value="1" />有 <input type="radio" name="'.$value['Field'].'" value="2" />无';
								} else if($_POST["c$f"] == 'textarea') {
									$code .='<span>'.$value['Comment'].'</span>    <textarea id="'.$value['Field'].'" rows="10" cols="40" name="'.$value['Field'].'"></textarea>';
					
								}
					    }
				
						//$code .='</li>';
						//echo '<br />';
					}
					if($_POST['adminc'] == 'on')
					    {
					$code .= '
					 <tr>
             <td><input name="jump_url" type="hidden"  value="__URL__/'.$tname[1].'index" /></td>
              <td>
                <input type="submit" name="Submit2" value="确定新增" class="inputButton" />
                <input type="reset" name="Reset" value="还原重填" class="inputButton" /></td>
            </tr>
					</form>
					</table>
<p class="i">红色标记的内容必须填写,<br />
 权限如果不想设置，请留空不选择</p>
        <br>
<include file="Public:footer" />';
					    }else {
					    	$code .='front add';
					    }
								
					break;
					case 'edit':
						if($_POST['adminc'] == 'on')
					    {
						$code .= '<tagLib name="html,cx" />
									<include file="Public:header" />
									<div class="bodyTitle">
									<div class="bodyTitleLeft"></div>
									 <div class="bodyTitleText">编辑</div>
									</div>
									<br><h3 class="marginbot"><a href="__URL__/'.$tname[1].'index" class="sgbtn">＋返回主类</a></h3>
									<table width="100%">
									<form id="form1" action="__URL__/upd_'.$tname[1].'" method="post">
								
					';
					    }else {
					    	$code .='front edit';
					    }
						foreach($field as $key=>$value)
						{

							$f = $value['Field'];
							if($_POST['adminc'] == 'on')
					    	{
						    	if($_POST["c$f"] =='input')
								{
									 $code .='<tr><td>'.$value['Comment'].'</td><td>    <input name="'.$value['Field'].'" value="{$vo.'.$value['Field'].'}" type="text"  /> </td></tr>';
								} else if($_POST["c$f"] =='select') {
									 $code .='<tr><td>'.$value['Comment'].'</td> <td>   <select name="'.$value['Field'].'"'.' class="w-100"><option value="0">系统根ID</option><volist name="list" id="svo"><option value="{$svo.id}"  <eq name="svo.id" value="$vo[\'parentid\']"> selected </eq> >{$svo.fulltitle}</option></volist></select></td></tr>';
								} else if($_POST["c$f"] =='radio'){
									 $code .='<tr><td>'.$value['Comment'].'</td><td>    <input type="radio" name="'.$value['Field'].'" value="1" />有 <input type="radio" name="'.$value['Field'].'" value="2" />无</td></tr>';
								} else if($_POST["c$f"] == 'textarea') {
									$code .='<tr><td>'.$value['Comment'].'</td><td>    <textarea id="'.$value['Field'].'" rows="10" cols="40" name="'.$value['Field'].'"></textarea></td></tr>';
					
								}
					    	}else {
					    		//front action
					    		$code .='front edit';
					    		
					    	}
						
				
					}
					if($_POST['adminc'] == 'on')
					    {
							$code .= '
					 <tr>
             <td><input name="jump_url" type="hidden"  value="__URL__/'.$tname[1].'index" /></td>
              <td>
                <input type="submit" name="Submit2" value="确定编辑" class="inputButton" />
                <input type="reset" name="Reset" value="还原重填" class="inputButton" /></td>
            </tr>
					</form>
					</table>
<p class="i">红色标记的内容必须填写,<br />
 权限如果不想设置，请留空不选择</p>
        <br>
<include file="Public:footer" />';
					    }else {
					    	$code .='front edit';
					    }
			
					
		
						}
					}
				echo nl2br(htmlentities($code,ENT_NOQUOTES,'utf-8'));
	}
	
		

public function index()
{
	$model = '';
	$field ='';
	//获取默认生成代码类型
	$acname ='';// Cookie::get('gnaction');

		
	//获取数据库表
	if(!empty($_REQUEST["mn"]))
	{
		$model = trim($_REQUEST["mn"]);
	}

	if(!empty($model))
	{
		$m = M(msubstr($model,3,count($model)));
		$field = $m->query("SHOW FULL FIELDS FROM ".$model);

	}
	$this->assign('dbname',$model);
	$this->assign('var',$field);

	$this->display();


}
}